package site.timers.game;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import java.util.ArrayList;
import java.util.List;

public class Character {
    private Texture texture;
    private int x, y;
    private int moveRange;
    private int health;
    private int attack;
    private int defense;
    private int magicResist; // 魔法抗性
    private int critRate; // 暴击率百分比
    private int dodgeRate; // 闪避率百分比
    private boolean isPlayer;
    private Equipment weapon;
    private Equipment armor;
    private Equipment accessory;
    private int level;
    private int exp;
    private int maxHealth;
    private List<Skill> skills;
    private int mana;
    private int maxMana;

    public Character(Texture texture, int x, int y, boolean isPlayer) {
        this.texture = texture;
        this.x = x;
        this.y = y;
        this.moveRange = 3;
        this.maxHealth = 100;
        this.health = maxHealth;
        this.attack = 10;
        this.defense = 5;
        this.magicResist = 3;
        this.critRate = 15;
        this.dodgeRate = 10;
        this.isPlayer = isPlayer;
        this.level = 1;
        this.exp = 0;
        this.maxMana = 50;
        this.mana = maxMana;
        this.skills = new ArrayList<>();
        if (isPlayer) {
            skills.add(new HealSkill());
            skills.add(new FireballSkill());
            skills.add(new LightningSkill());
        }
    }

    public void gainExp(int amount) {
        exp += amount;
        if (exp >= level * 100) {
            levelUp();
        }
    }

    private void levelUp() {
        level++;
        exp = 0;
        maxHealth += 10;
        health = maxHealth; // 升级时恢复全部生命值
        attack += 2;
        defense += 1;
        magicResist += 1;
        critRate = Math.min(critRate + 1, 30); // 暴击率上限30%
        dodgeRate = Math.min(dodgeRate + 1, 20); // 闪避率上限20%
    }

    public void render(SpriteBatch batch) {
        batch.draw(texture, x * 32, y * 32, 32, 32);
    }

    public boolean moveTo(int newX, int newY) {
        if (Math.abs(newX - x) + Math.abs(newY - y) <= moveRange) {
            x = newX;
            y = newY;
            return true;
        }
        return false;
    }

    public int attack(Character target) {
        // 检查闪避
        if (Math.random() * 100 < target.dodgeRate) {
            return -1; // 闪避
        }
        
        // 计算基础伤害
        int damage = Math.max(0, attack - target.defense);
        boolean isCrit = false;
        
        // 检查暴击
        if (Math.random() * 100 < critRate) {
            damage *= 2;
            isCrit = true;
        }
        
        target.health -= damage;
        return isCrit ? 1 : 0; // 0=普通, 1=暴击
    }

    // Getters and setters
    public int getX() { return x; }
    public int getY() { return y; }
    public int getHealth() { return health; }
    public boolean isPlayer() { return isPlayer; }
    public int getMoveRange() { return moveRange; }
    public int getDefense() { return defense; }
    public int getAttack() { return attack; }
    public int getLevel() { return level; }
    public int getExp() { return exp; }
    public int getMaxHealth() { return maxHealth; }
    public List<Skill> getSkills() { return skills; }
    public int getMana() { return mana; }
    public int getMaxMana() { return maxMana; }
    public int getMagicResist() { 
        return magicResist + (armor != null ? armor.getMagicResistBonus() : 0);
    }
    
    public void equip(Equipment equipment) {
        // 简单装备逻辑：根据名称判断装备类型
        if (equipment.getName().contains("剑") || equipment.getName().contains("杖")) {
            weapon = equipment;
        } else if (equipment.getName().contains("甲") || equipment.getName().contains("袍")) {
            armor = equipment;
        } else {
            accessory = equipment;
        }
    }
    
    public int getTotalAttack() {
        return attack + (weapon != null ? weapon.getAttackBonus() : 0);
    }
    
    public int getTotalDefense() {
        return defense + (armor != null ? armor.getDefenseBonus() : 0);
    }
    
    public Equipment getWeapon() { return weapon; }
    public Equipment getArmor() { return armor; }
    public Equipment getAccessory() { return accessory; }
    
    public void heal(int amount) {
        health = Math.min(health + amount, maxHealth);
    }
    
    public void takeDamage(int amount) {
        health -= amount;
    }
    
    public void takeMagicDamage(int amount) {
        int reducedDamage = Math.max(1, amount - magicResist);
        health -= reducedDamage;
    }
    
    public void restoreMana(int amount) {
        mana = Math.min(mana + amount, maxMana);
    }
    
    public void useSkill(Skill skill, Character target) {
        if (mana >= skill.getCost() && skill.isReady()) {
            skill.execute(this, target);
            mana -= skill.getCost();
        }
    }
    
    public void updateSkills() {
        for (Skill skill : skills) {
            skill.updateCooldown();
        }
    }
}